<%--
  Created by IntelliJ IDEA.
  User: 温海杰
  Date: 2020/6/17
  Time: 9:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>新闻管理后台</title>
</head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/infoStyle.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/wangEditor.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/wangEditor.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/editor.js"></script>

<style>
    #div {
        width: 900px;
        height: 800px;
        background: #fff;
        margin: 0px auto;
        margin-top: 30px;
    }
</style>
<script type="text/javascript">
    var path = '${pageContext.request.contextPath}/news/upload'
    var id = '${requestScope.get("id")}'
    $(function () {
        var path = '${pageContext.request.contextPath}/news/upload'


        $.post("${pageContext.request.contextPath}/news/getNewsInfo", {id: id}, function (data) {
            var id = data.id;
            var title = data.title;
            var body = data.body;
            var summaryInfo = data.summaryInfo;
            var writer = data.writer;
            var releaseDate = data.releaseDate;

            $('#newsId').val(id);
            $('#title').val(title);
            $('#s-editor').val(body);
            show(body);
            $('#summaryInfo').val(summaryInfo);
            $('#writer').val(writer);
            $('#releaseDate').text(releaseDate);
        }, "json")

        $("#newsId").val(id);
        //每5秒异步提交
        setInterval(function () {

            var title = $('#title').val();
            var body = $('#s-editor').val();
            var summaryInfo = $('#summaryInfo').val();
            var writer = $('#writer').val();
            var flag = '否';
            if ((title != "" || title != null) && (body.length>12)) {
                $.post("${pageContext.request.contextPath}/news/updateNews", {
                    id: id,
                    title: title,
                    body: body,
                    summaryInfo: summaryInfo,
                    writer: writer,
                    releaseStatus: flag,
                }, function (data) {
                    var msg = data.msg;
                    if (mag = '发布成功') {
                        $('#success').css("display", "block");
                    }
                }, "json")
            }
        }, 5000);
    });

    function modifyDate() {
        var id =  $('#newsId').val();
        var title = $('#title').val();
        var body =  $('#s-editor').val();
        var summaryInfo = $('#summaryInfo').val();
        var writer =   $('#writer').val();

        var releaseStatus ='是';
        $.post("${pageContext.request.contextPath}/news/releaseNews", {id: id,title:title,body:body,summaryInfo:summaryInfo,writer:writer,releaseStatus:releaseStatus}, function (data) {
            alert("修改成功！");
        }, "json")
    }
    function check() {
        var title = $('#title').val();
        var body = $('#s-editor').val();
        var summaryInfo = $('#summaryInfo').val();
        var writer = $('#writer').val();
        if(title==''||title==null){
            alert("标题不能为空！");
            return false;
        }
        if(body.length<12){
            alert("没有正文内容！");
            return false;
        }
        if(summaryInfo=''||summaryInfo==null){
            alert("摘要不能为空！");
            return false;
        }
        if(summaryInfo.length>=50){
            alert("摘要太长，请简短点！");
            return false;
        }
        if(writer=''||writer==null){
            alert("作者不能为空！");
            return false;
        }
        return true;
    }
</script>
<body style="background:grey;">
<div id="div">
    <form action="${pageContext.request.contextPath}/news/releaseNews" method="POST" id="data-form" onsubmit="return check()">
        <input type="text" id="newsId" name="id" style="display: none">
        <div>
            <label>标题</label><br>
            <input type="text" id="title" name="title" style="width:99%"/>
        </div>
        <div>
            <label>内容</label>
            <div id="editor">
            </div>
            <input name="body" id="s-editor" type="text" style="display:none;"/>
        </div>
        <div>
            <label>摘要</label>
            <div>
                <textarea style="width:99%;height:100px;resize:none;" name="summaryInfo" id="summaryInfo"></textarea>
            </div>
        </div>

        <div>
            <label>作者</label>
            <input type="text" name="writer" id="writer"/>
            <label>发布日期</label>
            <label id="releaseDate"></label>
        </div>
        <br>
        <div>
            <input class="button" type="submit" value="发布" style="background:grey;" id="modifyBtn" />
        </div>
        <input type="tetx" name="releaseStatus" value="是" style="display: none">
    </form>
</div>
</body>
</html>
